﻿Imports System.IO

Public Class TrackItem
    Public Sub New(itemNode As KujuReader.Node)
        TrItemId = itemNode.Nodes("TrItemId").Value
        TrItemSData = MultipleValues(itemNode.Nodes("TrItemSData").Value)
        TrItemRData = MultipleValues(itemNode.Nodes("TrItemRData").Value)
    End Sub

    Public Property TrItemId As String
    Public Property TrItemSData As String()
    Public Property TrItemRData As String()

    Protected Function MultipleValues(values As String) As String()
        Return values.Split(" "c)
    End Function
End Class

Public Class SidingItem
    Inherits TrackItem

    Public Sub New(itemNode As KujuReader.Node)
        MyBase.New(itemNode)
        SidingTrItemData = MultipleValues(itemNode.Nodes("SidingTrItemData").Value)
        SidingName = itemNode.Nodes("SidingItem").Value
    End Sub

    Public Property SidingTrItemData As String()
    Public Property SidingName As String
End Class

Public Class PlatformItem
    Inherits TrackItem

    Public Sub New(itemNode As KujuReader.Node)
        MyBase.New(itemNode)
        PlatformTrItemData = MultipleValues(itemNode.Nodes("PlatformTrItemData").Value)
        PlatformName = itemNode.Nodes("PlatformName").Value
        Station = itemNode.Nodes("Station").Value
        PlatformMinWaitingTime = itemNode.Nodes("PlatformMinWaitingTime").Value
        PlatformNumPassengersWaiting = itemNode.Nodes("PlatformNumPassengersWaiting").Value
    End Sub

    Public Property PlatformTrItemData As String()
    Public Property PlatformName As String
    Public Property Station As String
    Public Property PlatformMinWaitingTime As String
    Public Property PlatformNumPassengersWaiting As String
End Class

Public Class TrackInteractivesTableReader
    Private mTrackItems As New Dictionary(Of String, TrackItem)

    Public Sub New(ByVal fileOrRoutePath As String)

        fileOrRoutePath = fileOrRoutePath.ToLower
        If Not fileOrRoutePath.EndsWith("tit") Then
            Dim titFiles() As FileInfo = New DirectoryInfo(fileOrRoutePath).GetFiles("*.tit")
            If titFiles.Length = 0 Then
                Throw New FileNotFoundException(String.Format("Track Interactive Table (tit) file not found in {0}", fileOrRoutePath))
            End If
            fileOrRoutePath = titFiles(0).FullName
        End If
        ReadFile(New KujuReader(fileOrRoutePath))
    End Sub

    Public Sub New(kr As KujuReader)
        ReadFile(kr)
    End Sub

    Public Function TrackItem(TrItemId As String) As TrackItem
        Return mTrackItems.Item(TrItemId)
    End Function

    Public Function SidingItems() As IEnumerable(Of PlatformItem)
        Return mTrackItems.OfType(Of SidingItem)()
    End Function

    Public Function PlatformItems() As IEnumerable(Of PlatformItem)
        Return mTrackItems.OfType(Of PlatformItem)()
    End Function

    Private Sub ReadFile(kr As KujuReader)
        Dim ItemNode As KujuReader.Node
        Dim trackItem As TrackItem

        ItemNode = kr.Nodes("TrItemTable")
        For Each n As KujuReader.Node In ItemNode.Nodes
            Select Case n.Name
                Case "SidingItem"
                    trackItem = New SidingItem(n)
                Case "PlatformItem"
                    trackItem = New PlatformItem(n)
                Case Else
                    trackItem = New TrackItem(n)
            End Select
            mTrackItems.Add(trackItem.TrItemId, trackItem)
        Next
    End Sub

End Class
